<?php
namespace Admin\Model;
use Think\Model;

class FieldModel extends Model{
	//查询总记录
	public function total(){
		return $this->where($this->map())->count();
	}
	
	//查询所有
	public function all($firstRow){
		return $this->field('id,name,selected')
    				->where($this->map())
    				->order(array('id'=>'ASC'))
    				->limit($firstRow,C('PAGE_SIZE'))
    				->select();
	}
	
	//查询所有（不分页）
	public function all2(){
		return $this->field('id,name,selected')
    				->order(array('id'=>'ASC'))
    				->select();
	}
	
	//查询所有（默认字段）
	public function all3(){
		$map['selected'] = 1;
		return $this->field('id')
					->where($map)
					->order(array('id'=>'ASC'))
					->select();
	}
	
	//查询一条
	public function one(){
		$map['id'] = I('get.id');
		return $this->field('selected')->where($map)->find();
	}
	
	//设置和取消默认
	public function selected($selected){
		$data = array(
			'id'=>I('get.id'),
			'selected'=>$selected
		);
		return $this->save($data);
	}
	
	//搜索
	private function map(){
		$map = array(
			'_complex'=>array(
				'name'=>array('like','%'.I('get.keyword').'%'),
				'_logic'=>'OR'
			)
		);
		return $map;
	}
}